Java处理时间加减天数加减月 发表于 2021-09-24 | 次阅读 字数统计: 450 字 | 阅读时长 ≈ 3 min 12345678910111213141516171819202122232425262728293031323334/**** * 传入具体日期 ,返回具体日期增加一个月。 * @param date 日期(2017-04-13) * @return 2017-05-13 * @throws ParseException */ private String calculateMonth(String date) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date dt = sdf.parse(date); Calendar rightNow = Calendar.getInstance(); rightNow.setTime(dt); rightNow.add(Calendar.MONTH, 1); Date dt1 = rightNow.getTime(); String reStr = sdf.format(dt1); return reStr; }/**** * 传入具体日期 ,返回具体日期减少一天 * @param date 日期(2017-04-13) * @return 2017-04-12 * @throws ParseException */ private String calculateDay(String date) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date dt = sdf.parse(date); Calendar rightNow = Calendar.getInstance(); rightNow.setTime(dt); rightNow.add(Calendar.DATE, -1); Date dt1 = rightNow.getTime(); String reStr = sdf.format(dt1); return reStr; } 1234567891011121314151617181920212223242526/** * 传入具体日期 ,返回具体日期增加或者减少天数,传出日期 * @author wjk * @Date 2021-09-27 */private Date calculateDay(Date date,int day){ Calendar rightNow = Calendar.getInstance(); rightNow.setTime(date); rightNow.add(Calendar.DATE, day); Date date1 = rightNow.getTime(); return date1;}/** * 传入具体日期 ,返回具体日期增加或者减少月数,传出日期 * @author wjk * @Date 2021-09-27 */private Date calculateMonth(Date date,int month){ Calendar rightNow = Calendar.getInstance(); rightNow.setTime(date); rightNow.add(Calendar.MONTH, month); Date date1 = rightNow.getTime(); return date1;} 12345678910/** * 传入具体日期 ,返回当月的天数 * @author wjk * @Date 2021-09-28 */public static int getDaysOfMonth(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.getActualMaximum(Calendar.DAY_OF_MONTH); } 12345678910111213141516171819/** * 获取当月最后几天 * @param days * @return */ private static List<String> getLastDays(int days) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Calendar calendar = Calendar.getInstance(); List<String> list=new ArrayList<String>(); for(int i=1;i<=days;i++){ calendar.add(Calendar.MONTH, 1); calendar.set(Calendar.DATE, 1); calendar.add(Calendar.DATE, -i); Date theDate = calendar.getTime(); String s = df.format(theDate); list.add(s); } return list; }} 欢迎加我的微信,共同交流技术